package com.FH.organization.operator_log;

import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 日志实现接口
 * Created by acer on 2016/7/18.
 */
@Stateless
public class OperatorLogDAOImpl extends AccessDB implements OperatorLogDAO {



    //查询日志信息
    public ResultDTO getOperatorLogInfo(String search, String page, String size){

        ResultDTO resultDTO = new ResultDTO();

        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(1) from operator_log where 1=1 ";

            String sql_w ="";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if(page.equals("1")){
                sql_page = " limit 0,"+size+"";
            }else{
                sql_page = " limit "+((page_ok-1)*size_ok)+","+size+"";
            }

            if(json.has("operator_log_id")){
                sql_w += " AND operator_log_id = '"+json.getString("operator_log_id")+"'";
            }
            if(json.has("member_id")){
                sql_w += " AND member_id = '"+json.getString("member_id")+"'";
            }
            if(json.has("work_form_id")){
                sql_w += " AND work_form_id = '"+json.getString("work_form_id")+"'";
            }
            if(json.has("operator_log_type")){
                sql_w += " AND operator_log_type = '"+json.getString("operator_log_type")+"'";
            }
            String sql_ok = sqlexit+sql_w ;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String []> seresult = new ArrayList<String []>();

            List<OperatorLogShowDTO> result = new ArrayList<OperatorLogShowDTO>();
            if (!existName.equals("0")){
                  String sql="select operator_log_id,member_id,is_delete,operator_log_content,operator_log_time,operator_log_title," +
                          "work_form_id,operator_log_type from operator_log where 1=1 "+sql_w+" ORDER BY operator_log_time DESC "+ sql_page;//
                  seresult = this.getReportingTo(sql);
                //构建返回对象
                for (int i = 0; i < seresult.size(); i++){

                    OperatorLogShowDTO operatorlog=new OperatorLogShowDTO();
                    operatorlog.setOperator_log_id(seresult.get(i)[1]);
                    operatorlog.setMember_id(seresult.get(i)[2]);
                    operatorlog.setIs_delete(seresult.get(i)[3]);
                    operatorlog.setOperator_log_content(seresult.get(i)[4]);
                    operatorlog.setOperator_log_time(seresult.get(i)[5]);
                    operatorlog.setOperator_log_title(seresult.get(i)[6]);
                    //operatorlog.setMember_name(seresult.get(i)[7]);
                    operatorlog.setWork_form_id(seresult.get(i)[7]);
                    operatorlog.setOperator_log_type(seresult.get(i)[8]);

                    result.add(operatorlog);
                }
            }

            if(seresult.size()!=0) {
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion("日志信息调用成功");
                resultDTO.setStr(result.size()+"");
                resultDTO.setValues(result);
            }else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion("日志信息不存在");
            }

        }catch (JSONException e){
            e.printStackTrace();
        }
        return resultDTO;
    };

    //增加日志信息
    public ResultDTO addOperatorLogInfo(OperatorLogPullDTO  operatorlogPullDTO){
       // String sql_isHas = "select count(1) from operator_log WHERE operator_log_title='"+operatorlogPullDTO.getOperator_log_title()+"'";

      //  String existOperatorlog= getReportingToOne(sql_isHas);

        int resultls = 0;

        //构建返回对象
        ResultDTO resultDTO = new ResultDTO();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//日期格式化

        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

       // if(existOperatorlog.equals("0")){
            String operatorlog_insert="";
                   operatorlog_insert+="insert into operator_log (operator_log_id,operator_log_title,operator_log_content,member_id,operator_log_time,is_delete,work_form_id,operator_log_type)" +
                           "values ('"+uuid+"','"+operatorlogPullDTO.getOperator_log_title()+"','"+operatorlogPullDTO.getOperator_log_content()+"','"+operatorlogPullDTO.getMember_id()+"','"+df.format(new Date())+"'" +
                           ",'"+0+"','"+operatorlogPullDTO.getWork_form_id()+"','"+operatorlogPullDTO.getOperator_log_type()+"')";
            //插入业务表
            resultls =  addReportingTo(operatorlog_insert);
       // }

        if(resultls!=0) {
            resultDTO.setStr(uuid);
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion("日志信息插入成功");
        }else{
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion("日志信息插入失败");
        }

        return resultDTO;

    };;

//    public ResultDTO updateOperatorLogInfo(OperatorLogPullDTO  operatorlogPullDTO){
//        return null;
//    };;

    //删除日志信息
    public ResultDTO deleteOperatorLogInfo(OperatorLogPullDTO  operatorlogPullDTO){

        //删除之前先去查一遍该表是否有记录
        String sql = "select count(1) from operator_log where operator_log_id='"+operatorlogPullDTO.getOperator_log_id()+"'";

        String existName = getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();

        if(existName!=null){
            String QUERY = "";
            QUERY += "delete from operator_log where operator_log_id='"+operatorlogPullDTO.getOperator_log_id()+"'";
            result = this.addReportingTo(QUERY);
        }

        if(result!= 0){
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion("调用成功，删除成功");
        }else{
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion("调用失败，删除失败");

        }
        return resultDTO;
    };;
}

//更新日志信息